<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>5. Expressions &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="The Python Language Reference" href="index.html" />
    <link rel="next" title="6. Simple statements" href="simple_stmts.html" />
    <link rel="prev" title="4. Execution model" href="executionmodel.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="simple_stmts.html" title="6. Simple statements"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="executionmodel.html" title="4. Execution model"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" accesskey="U">The Python Language Reference</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="expressions">
<span id="id1"></span><h1>5. Expressions<a class="headerlink" href="#expressions" title="Permalink to this headline">¶</a></h1>
<p id="index-880">This chapter explains the meaning of the elements of expressions in Python.</p>
<p id="index-881"><strong>Syntax Notes:</strong> In this and the following chapters, extended BNF notation will
be used to describe syntax, not lexical analysis.  When (one alternative of) a
syntax rule has the form</p>
<pre>
<strong id="grammar-token-name">name</strong> ::=  <tt class="xref docutils literal"><span class="pre">othername</span></tt>
</pre>
<p id="index-882">and no semantics are given, the semantics of this form of <tt class="docutils literal"><span class="pre">name</span></tt> are the same
as for <tt class="docutils literal"><span class="pre">othername</span></tt>.</p>
<div class="section" id="arithmetic-conversions">
<span id="conversions"></span><h2>5.1. Arithmetic conversions<a class="headerlink" href="#arithmetic-conversions" title="Permalink to this headline">¶</a></h2>
<p id="index-883">When a description of an arithmetic operator below uses the phrase &#8220;the numeric
arguments are converted to a common type,&#8221; the arguments are coerced using the
coercion rules listed at  <a class="reference external" href="datamodel.html#coercion-rules"><em>Coercion rules</em></a>.  If both arguments are standard
numeric types, the following coercions are applied:</p>
<ul class="simple">
<li>If either argument is a complex number, the other is converted to complex;</li>
<li>otherwise, if either argument is a floating point number, the other is
converted to floating point;</li>
<li>otherwise, if either argument is a long integer, the other is converted to
long integer;</li>
<li>otherwise, both must be plain integers and no conversion is necessary.</li>
</ul>
<p>Some additional rules apply for certain operators (e.g., a string left argument
to the &#8216;%&#8217; operator). Extensions can define their own coercions.</p>
</div>
<div class="section" id="atoms">
<span id="id2"></span><h2>5.2. Atoms<a class="headerlink" href="#atoms" title="Permalink to this headline">¶</a></h2>
<p id="index-884">Atoms are the most basic elements of expressions.  The simplest atoms are
identifiers or literals.  Forms enclosed in reverse quotes or in parentheses,
brackets or braces are also categorized syntactically as atoms.  The syntax for
atoms is:</p>
<pre>
<strong id="grammar-token-atom">atom     </strong> ::=  <a class="reference external" href="lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a> | <a class="reference internal" href="#grammar-token-literal"><tt class="xref docutils literal"><span class="pre">literal</span></tt></a> | <a class="reference internal" href="#grammar-token-enclosure"><tt class="xref docutils literal"><span class="pre">enclosure</span></tt></a>
<strong id="grammar-token-enclosure">enclosure</strong> ::=  <a class="reference internal" href="#grammar-token-parenth_form"><tt class="xref docutils literal"><span class="pre">parenth_form</span></tt></a> | <a class="reference internal" href="#grammar-token-list_display"><tt class="xref docutils literal"><span class="pre">list_display</span></tt></a>
               | <a class="reference internal" href="#grammar-token-generator_expression"><tt class="xref docutils literal"><span class="pre">generator_expression</span></tt></a> | <a class="reference internal" href="#grammar-token-dict_display"><tt class="xref docutils literal"><span class="pre">dict_display</span></tt></a>
               | <a class="reference internal" href="#grammar-token-string_conversion"><tt class="xref docutils literal"><span class="pre">string_conversion</span></tt></a> | <a class="reference internal" href="#grammar-token-yield_atom"><tt class="xref docutils literal"><span class="pre">yield_atom</span></tt></a>
</pre>
<div class="section" id="atom-identifiers">
<span id="identifiers-names"></span><h3>5.2.1. Identifiers (Names)<a class="headerlink" href="#atom-identifiers" title="Permalink to this headline">¶</a></h3>
<p id="index-885">An identifier occurring as an atom is a name.  See section <a class="reference external" href="lexical_analysis.html#identifiers"><em>Identifiers and keywords</em></a>
for lexical definition and section <a class="reference external" href="executionmodel.html#naming"><em>Naming and binding</em></a> for documentation of naming and
binding.</p>
<p id="index-886">When the name is bound to an object, evaluation of the atom yields that object.
When a name is not bound, an attempt to evaluate it raises a <a title="exceptions.NameError" class="reference external" href="../library/exceptions.html#exceptions.NameError"><tt class="xref docutils literal"><span class="pre">NameError</span></tt></a>
exception.</p>
<p id="index-887"><strong>Private name mangling:</strong> When an identifier that textually occurs in a class
definition begins with two or more underscore characters and does not end in two
or more underscores, it is considered a <em>private name</em> of that class.
Private names are transformed to a longer form before code is generated for
them.  The transformation inserts the class name in front of the name, with
leading underscores removed, and a single underscore inserted in front of the
class name.  For example, the identifier <tt class="docutils literal"><span class="pre">__spam</span></tt> occurring in a class named
<tt class="docutils literal"><span class="pre">Ham</span></tt> will be transformed to <tt class="docutils literal"><span class="pre">_Ham__spam</span></tt>.  This transformation is
independent of the syntactical context in which the identifier is used.  If the
transformed name is extremely long (longer than 255 characters), implementation
defined truncation may happen.  If the class name consists only of underscores,
no transformation is done.</p>
</div>
<div class="section" id="literals">
<span id="atom-literals"></span><h3>5.2.2. Literals<a class="headerlink" href="#literals" title="Permalink to this headline">¶</a></h3>
<p id="index-888">Python supports string literals and various numeric literals:</p>
<pre>
<strong id="grammar-token-literal">literal</strong> ::=  <a class="reference external" href="lexical_analysis.html#grammar-token-stringliteral"><tt class="xref docutils literal"><span class="pre">stringliteral</span></tt></a> | <a class="reference external" href="lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a> | <a class="reference external" href="lexical_analysis.html#grammar-token-longinteger"><tt class="xref docutils literal"><span class="pre">longinteger</span></tt></a>
             | <a class="reference external" href="lexical_analysis.html#grammar-token-floatnumber"><tt class="xref docutils literal"><span class="pre">floatnumber</span></tt></a> | <a class="reference external" href="lexical_analysis.html#grammar-token-imagnumber"><tt class="xref docutils literal"><span class="pre">imagnumber</span></tt></a>
</pre>
<p>Evaluation of a literal yields an object of the given type (string, integer,
long integer, floating point number, complex number) with the given value.  The
value may be approximated in the case of floating point and imaginary (complex)
literals.  See section <a class="reference external" href="lexical_analysis.html#literals"><em>Literals</em></a> for details.</p>
<p id="index-889">All literals correspond to immutable data types, and hence the object&#8217;s identity
is less important than its value.  Multiple evaluations of literals with the
same value (either the same occurrence in the program text or a different
occurrence) may obtain the same object or a different object with the same
value.</p>
</div>
<div class="section" id="parenthesized-forms">
<span id="parenthesized"></span><h3>5.2.3. Parenthesized forms<a class="headerlink" href="#parenthesized-forms" title="Permalink to this headline">¶</a></h3>
<p id="index-890">A parenthesized form is an optional expression list enclosed in parentheses:</p>
<pre>
<strong id="grammar-token-parenth_form">parenth_form</strong> ::=  &quot;(&quot; [<a class="reference internal" href="#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a>] &quot;)&quot;
</pre>
<p>A parenthesized expression list yields whatever that expression list yields: if
the list contains at least one comma, it yields a tuple; otherwise, it yields
the single expression that makes up the expression list.</p>
<p id="index-891">An empty pair of parentheses yields an empty tuple object.  Since tuples are
immutable, the rules for literals apply (i.e., two occurrences of the empty
tuple may or may not yield the same object).</p>
<p id="index-892">Note that tuples are not formed by the parentheses, but rather by use of the
comma operator.  The exception is the empty tuple, for which parentheses <em>are</em>
required &#8212; allowing unparenthesized &#8220;nothing&#8221; in expressions would cause
ambiguities and allow common typos to pass uncaught.</p>
</div>
<div class="section" id="list-displays">
<span id="lists"></span><h3>5.2.4. List displays<a class="headerlink" href="#list-displays" title="Permalink to this headline">¶</a></h3>
<p id="index-893">A list display is a possibly empty series of expressions enclosed in square
brackets:</p>
<pre>
<strong id="grammar-token-list_display">list_display       </strong> ::=  &quot;[&quot; [<a class="reference internal" href="#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a> | <a class="reference internal" href="#grammar-token-list_comprehension"><tt class="xref docutils literal"><span class="pre">list_comprehension</span></tt></a>] &quot;]&quot;
<strong id="grammar-token-list_comprehension">list_comprehension </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> <a class="reference internal" href="#grammar-token-list_for"><tt class="xref docutils literal"><span class="pre">list_for</span></tt></a>
<strong id="grammar-token-list_for">list_for           </strong> ::=  &quot;for&quot; <a class="reference external" href="simple_stmts.html#grammar-token-target_list"><tt class="xref docutils literal"><span class="pre">target_list</span></tt></a> &quot;in&quot; <a class="reference internal" href="#grammar-token-old_expression_list"><tt class="xref docutils literal"><span class="pre">old_expression_list</span></tt></a> [<a class="reference internal" href="#grammar-token-list_iter"><tt class="xref docutils literal"><span class="pre">list_iter</span></tt></a>]
<strong id="grammar-token-old_expression_list">old_expression_list</strong> ::=  <a class="reference internal" href="#grammar-token-old_expression"><tt class="xref docutils literal"><span class="pre">old_expression</span></tt></a> [(&quot;,&quot; <a class="reference internal" href="#grammar-token-old_expression"><tt class="xref docutils literal"><span class="pre">old_expression</span></tt></a>)+ [&quot;,&quot;]]
<strong id="grammar-token-list_iter">list_iter          </strong> ::=  <a class="reference internal" href="#grammar-token-list_for"><tt class="xref docutils literal"><span class="pre">list_for</span></tt></a> | <a class="reference internal" href="#grammar-token-list_if"><tt class="xref docutils literal"><span class="pre">list_if</span></tt></a>
<strong id="grammar-token-list_if">list_if            </strong> ::=  &quot;if&quot; <a class="reference internal" href="#grammar-token-old_expression"><tt class="xref docutils literal"><span class="pre">old_expression</span></tt></a> [<a class="reference internal" href="#grammar-token-list_iter"><tt class="xref docutils literal"><span class="pre">list_iter</span></tt></a>]
</pre>
<p id="index-894">A list display yields a new list object.  Its contents are specified by
providing either a list of expressions or a list comprehension.  When a
comma-separated list of expressions is supplied, its elements are evaluated from
left to right and placed into the list object in that order.  When a list
comprehension is supplied, it consists of a single expression followed by at
least one <a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> clause and zero or more <a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> or <a class="reference external" href="compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a>
clauses.  In this case, the elements of the new list are those that would be
produced by considering each of the <a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> or <a class="reference external" href="compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> clauses a
block, nesting from left to right, and evaluating the expression to produce a
list element each time the innermost block is reached <a class="footnote-reference" href="#id19" id="id3">[1]</a>.</p>
</div>
<div class="section" id="generator-expressions">
<span id="genexpr"></span><h3>5.2.5. Generator expressions<a class="headerlink" href="#generator-expressions" title="Permalink to this headline">¶</a></h3>
<p id="index-895">A generator expression is a compact generator notation in parentheses:</p>
<pre>
<strong id="grammar-token-generator_expression">generator_expression</strong> ::=  &quot;(&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> <a class="reference internal" href="#grammar-token-genexpr_for"><tt class="xref docutils literal"><span class="pre">genexpr_for</span></tt></a> &quot;)&quot;
<strong id="grammar-token-genexpr_for">genexpr_for         </strong> ::=  &quot;for&quot; <a class="reference external" href="simple_stmts.html#grammar-token-target_list"><tt class="xref docutils literal"><span class="pre">target_list</span></tt></a> &quot;in&quot; <a class="reference internal" href="#grammar-token-or_test"><tt class="xref docutils literal"><span class="pre">or_test</span></tt></a> [<a class="reference internal" href="#grammar-token-genexpr_iter"><tt class="xref docutils literal"><span class="pre">genexpr_iter</span></tt></a>]
<strong id="grammar-token-genexpr_iter">genexpr_iter        </strong> ::=  <a class="reference internal" href="#grammar-token-genexpr_for"><tt class="xref docutils literal"><span class="pre">genexpr_for</span></tt></a> | <a class="reference internal" href="#grammar-token-genexpr_if"><tt class="xref docutils literal"><span class="pre">genexpr_if</span></tt></a>
<strong id="grammar-token-genexpr_if">genexpr_if          </strong> ::=  &quot;if&quot; <a class="reference internal" href="#grammar-token-old_expression"><tt class="xref docutils literal"><span class="pre">old_expression</span></tt></a> [<a class="reference internal" href="#grammar-token-genexpr_iter"><tt class="xref docutils literal"><span class="pre">genexpr_iter</span></tt></a>]
</pre>
<p id="index-896">A generator expression yields a new generator object.  It consists of a single
expression followed by at least one <a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> clause and zero or more
<a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> or <a class="reference external" href="compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> clauses.  The iterating values of the new
generator are those that would be produced by considering each of the
<a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> or <a class="reference external" href="compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> clauses a block, nesting from left to right, and
evaluating the expression to yield a value that is reached the innermost block
for each iteration.</p>
<p>Variables used in the generator expression are evaluated lazily in a separate
scope when the <a title="next" class="reference external" href="../library/functions.html#next"><tt class="xref docutils literal"><span class="pre">next()</span></tt></a> method is called for the generator object (in the
same fashion as for normal generators).  However, the <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> expression
of the leftmost <a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> clause is immediately evaluated in the current
scope so that an error produced by it can be seen before any other possible
error in the code that handles the generator expression.  Subsequent
<a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> and <a class="reference external" href="compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> clauses cannot be evaluated immediately since
they may depend on the previous <a class="reference external" href="compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> loop.  For example:
<tt class="docutils literal"><span class="pre">(x*y</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">range(10)</span> <span class="pre">for</span> <span class="pre">y</span> <span class="pre">in</span> <span class="pre">bar(x))</span></tt>.</p>
<p>The parentheses can be omitted on calls with only one argument. See section
<a class="reference internal" href="#calls"><em>Calls</em></a> for the detail.</p>
</div>
<div class="section" id="dictionary-displays">
<span id="dict"></span><h3>5.2.6. Dictionary displays<a class="headerlink" href="#dictionary-displays" title="Permalink to this headline">¶</a></h3>
<span class="target" id="index-897"></span><p id="index-898">A dictionary display is a possibly empty series of key/datum pairs enclosed in
curly braces:</p>
<pre>
<strong id="grammar-token-dict_display">dict_display  </strong> ::=  &quot;{&quot; [<a class="reference internal" href="#grammar-token-key_datum_list"><tt class="xref docutils literal"><span class="pre">key_datum_list</span></tt></a>] &quot;}&quot;
<strong id="grammar-token-key_datum_list">key_datum_list</strong> ::=  <a class="reference internal" href="#grammar-token-key_datum"><tt class="xref docutils literal"><span class="pre">key_datum</span></tt></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-key_datum"><tt class="xref docutils literal"><span class="pre">key_datum</span></tt></a>)* [&quot;,&quot;]
<strong id="grammar-token-key_datum">key_datum     </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> &quot;:&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>
</pre>
<p id="index-899">A dictionary display yields a new dictionary object.</p>
<p>The key/datum pairs are evaluated from left to right to define the entries of
the dictionary: each key object is used as a key into the dictionary to store
the corresponding datum.</p>
<p id="index-900">Restrictions on the types of the key values are listed earlier in section
<a class="reference external" href="datamodel.html#types"><em>The standard type hierarchy</em></a>.  (To summarize, the key type should be <a class="reference external" href="../glossary.html#term-hashable"><em class="xref">hashable</em></a>, which excludes
all mutable objects.)  Clashes between duplicate keys are not detected; the last
datum (textually rightmost in the display) stored for a given key value
prevails.</p>
</div>
<div class="section" id="string-conversions">
<span id="id4"></span><h3>5.2.7. String conversions<a class="headerlink" href="#string-conversions" title="Permalink to this headline">¶</a></h3>
<p id="index-901">A string conversion is an expression list enclosed in reverse (a.k.a. backward)
quotes:</p>
<pre>
<strong id="grammar-token-string_conversion">string_conversion</strong> ::=  &quot;'&quot; <a class="reference internal" href="#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a> &quot;'&quot;
</pre>
<p>A string conversion evaluates the contained expression list and converts the
resulting object into a string according to rules specific to its type.</p>
<p>If the object is a string, a number, <tt class="xref docutils literal"><span class="pre">None</span></tt>, or a tuple, list or dictionary
containing only objects whose type is one of these, the resulting string is a
valid Python expression which can be passed to the built-in function
<a title="eval" class="reference external" href="../library/functions.html#eval"><tt class="xref docutils literal"><span class="pre">eval()</span></tt></a> to yield an expression with the same value (or an approximation, if
floating point numbers are involved).</p>
<p>(In particular, converting a string adds quotes around it and converts &#8220;funny&#8221;
characters to escape sequences that are safe to print.)</p>
<p id="index-902">Recursive objects (for example, lists or dictionaries that contain a reference
to themselves, directly or indirectly) use <tt class="docutils literal"><span class="pre">...</span></tt> to indicate a recursive
reference, and the result cannot be passed to <a title="eval" class="reference external" href="../library/functions.html#eval"><tt class="xref docutils literal"><span class="pre">eval()</span></tt></a> to get an equal value
(<a title="exceptions.SyntaxError" class="reference external" href="../library/exceptions.html#exceptions.SyntaxError"><tt class="xref docutils literal"><span class="pre">SyntaxError</span></tt></a> will be raised instead).</p>
<p id="index-903">The built-in function <a title="repr" class="reference external" href="../library/functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a> performs exactly the same conversion in its
argument as enclosing it in parentheses and reverse quotes does.  The built-in
function <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str()</span></tt></a> performs a similar but more user-friendly conversion.</p>
</div>
<div class="section" id="yield-expressions">
<span id="yieldexpr"></span><h3>5.2.8. Yield expressions<a class="headerlink" href="#yield-expressions" title="Permalink to this headline">¶</a></h3>
<pre id="index-904">
<strong id="grammar-token-yield_atom">yield_atom      </strong> ::=  &quot;(&quot; <a class="reference internal" href="#grammar-token-yield_expression"><tt class="xref docutils literal"><span class="pre">yield_expression</span></tt></a> &quot;)&quot;
<strong id="grammar-token-yield_expression">yield_expression</strong> ::=  &quot;yield&quot; [<a class="reference internal" href="#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a>]
</pre>
<p>
<span class="versionmodified">New in version 2.5.</span></p>
<p>The <a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression is only used when defining a generator function,
and can only be used in the body of a function definition. Using a
<a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression in a function definition is sufficient to cause that
definition to create a generator function instead of a normal function.</p>
<p>When a generator function is called, it returns an iterator known as a
generator.  That generator then controls the execution of a generator function.
The execution starts when one of the generator&#8217;s methods is called.  At that
time, the execution proceeds to the first <a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression, where it
is suspended again, returning the value of <a class="reference internal" href="#grammar-token-expression_list"><strong class="xref">expression_list</strong></a> to
generator&#8217;s caller.  By suspended we mean that all local state is retained,
including the current bindings of local variables, the instruction pointer, and
the internal evaluation stack.  When the execution is resumed by calling one of
the generator&#8217;s methods, the function can proceed exactly as if the
<a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression was just another external call. The value of the
<a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression after resuming depends on the method which resumed
the execution.</p>
<p id="index-905">All of this makes generator functions quite similar to coroutines; they yield
multiple times, they have more than one entry point and their execution can be
suspended.  The only difference is that a generator function cannot control
where should the execution continue after it yields; the control is always
transfered to the generator&#8217;s caller.</p>
<p id="index-906">The following generator&#8217;s methods can be used to control the execution of a
generator function:</p>
<span class="target" id="index-907"></span><dl class="method">
<dt id="generator.next">
<tt class="descclassname">generator.</tt><tt class="descname">next</tt><big>(</big><big>)</big><a class="headerlink" href="#generator.next" title="Permalink to this definition">¶</a></dt>
<dd>Starts the execution of a generator function or resumes it at the last executed
<a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression.  When a generator function is resumed with a
<a title="next" class="reference external" href="../library/functions.html#next"><tt class="xref docutils literal"><span class="pre">next()</span></tt></a> method, the current <a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression always evaluates to
<a title="None" class="reference external" href="../library/constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.  The execution then continues to the next <a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a>
expression, where the generator is suspended again, and the value of the
<a class="reference internal" href="#grammar-token-expression_list"><strong class="xref">expression_list</strong></a> is returned to <a title="next" class="reference external" href="../library/functions.html#next"><tt class="xref docutils literal"><span class="pre">next()</span></tt></a>&#8216;s caller. If the generator
exits without yielding another value, a <a title="exceptions.StopIteration" class="reference external" href="../library/exceptions.html#exceptions.StopIteration"><tt class="xref docutils literal"><span class="pre">StopIteration</span></tt></a> exception is
raised.</dd></dl>

<dl class="method">
<dt id="generator.send">
<tt class="descclassname">generator.</tt><tt class="descname">send</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#generator.send" title="Permalink to this definition">¶</a></dt>
<dd>Resumes the execution and &#8220;sends&#8221; a value into the generator function.  The
<tt class="docutils literal"><span class="pre">value</span></tt> argument becomes the result of the current <a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a>
expression.  The <tt class="xref docutils literal"><span class="pre">send()</span></tt> method returns the next value yielded by the
generator, or raises <a title="exceptions.StopIteration" class="reference external" href="../library/exceptions.html#exceptions.StopIteration"><tt class="xref docutils literal"><span class="pre">StopIteration</span></tt></a> if the generator exits without
yielding another value. When <tt class="xref docutils literal"><span class="pre">send()</span></tt> is called to start the generator, it
must be called with <a title="None" class="reference external" href="../library/constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> as the argument, because there is no
<a class="reference external" href="simple_stmts.html#yield"><tt class="xref docutils literal"><span class="pre">yield</span></tt></a> expression that could receive the value.</dd></dl>

<dl class="method">
<dt id="generator.throw">
<tt class="descclassname">generator.</tt><tt class="descname">throw</tt><big>(</big><em>type</em><span class="optional">[</span>, <em>value</em><span class="optional">[</span>, <em>traceback</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#generator.throw" title="Permalink to this definition">¶</a></dt>
<dd>Raises an exception of type <tt class="docutils literal"><span class="pre">type</span></tt> at the point where generator was paused,
and returns the next value yielded by the generator function.  If the generator
exits without yielding another value, a <a title="exceptions.StopIteration" class="reference external" href="../library/exceptions.html#exceptions.StopIteration"><tt class="xref docutils literal"><span class="pre">StopIteration</span></tt></a> exception is
raised.  If the generator function does not catch the passed-in exception, or
raises a different exception, then that exception propagates to the caller.</dd></dl>

<span class="target" id="index-908"></span><dl class="method">
<dt id="generator.close">
<tt class="descclassname">generator.</tt><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#generator.close" title="Permalink to this definition">¶</a></dt>
<dd>Raises a <a title="exceptions.GeneratorExit" class="reference external" href="../library/exceptions.html#exceptions.GeneratorExit"><tt class="xref docutils literal"><span class="pre">GeneratorExit</span></tt></a> at the point where the generator function was
paused.  If the generator function then raises <a title="exceptions.StopIteration" class="reference external" href="../library/exceptions.html#exceptions.StopIteration"><tt class="xref docutils literal"><span class="pre">StopIteration</span></tt></a> (by exiting
normally, or due to already being closed) or <a title="exceptions.GeneratorExit" class="reference external" href="../library/exceptions.html#exceptions.GeneratorExit"><tt class="xref docutils literal"><span class="pre">GeneratorExit</span></tt></a> (by not
catching the exception), close returns to its caller.  If the generator yields a
value, a <a title="exceptions.RuntimeError" class="reference external" href="../library/exceptions.html#exceptions.RuntimeError"><tt class="xref docutils literal"><span class="pre">RuntimeError</span></tt></a> is raised.  If the generator raises any other
exception, it is propagated to the caller.  <tt class="xref docutils literal"><span class="pre">close()</span></tt> does nothing if the
generator has already exited due to an exception or normal exit.</dd></dl>

<p>Here is a simple example that demonstrates the behavior of generators and
generator functions:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">echo</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="s">&quot;Execution starts when &#39;next()&#39; is called for the first time.&quot;</span>
<span class="gp">... </span>    <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>                <span class="n">value</span> <span class="o">=</span> <span class="p">(</span><span class="k">yield</span> <span class="n">value</span><span class="p">)</span>
<span class="gp">... </span>            <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
<span class="gp">... </span>                <span class="n">value</span> <span class="o">=</span> <span class="n">e</span>
<span class="gp">... </span>    <span class="k">finally</span><span class="p">:</span>
<span class="gp">... </span>        <span class="k">print</span> <span class="s">&quot;Don&#39;t forget to clean up when &#39;close()&#39; is called.&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generator</span> <span class="o">=</span> <span class="n">echo</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">generator</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="go">Execution starts when &#39;next()&#39; is called for the first time.</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">generator</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">generator</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mf">2</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generator</span><span class="o">.</span><span class="n">throw</span><span class="p">(</span><span class="ne">TypeError</span><span class="p">,</span> <span class="s">&quot;spam&quot;</span><span class="p">)</span>
<span class="go">TypeError(&#39;spam&#39;,)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generator</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="go">Don&#39;t forget to clean up when &#39;close()&#39; is called.</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-909"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0342"><strong>PEP 0342</strong></a> - Coroutines via Enhanced Generators</dt>
<dd>The proposal to enhance the API and syntax of generators, making them usable as
simple coroutines.</dd>
</dl>
</div>
</div>
</div>
<div class="section" id="primaries">
<span id="id5"></span><h2>5.3. Primaries<a class="headerlink" href="#primaries" title="Permalink to this headline">¶</a></h2>
<p id="index-910">Primaries represent the most tightly bound operations of the language. Their
syntax is:</p>
<pre>
<strong id="grammar-token-primary">primary</strong> ::=  <a class="reference internal" href="#grammar-token-atom"><tt class="xref docutils literal"><span class="pre">atom</span></tt></a> | <a class="reference internal" href="#grammar-token-attributeref"><tt class="xref docutils literal"><span class="pre">attributeref</span></tt></a> | <a class="reference internal" href="#grammar-token-subscription"><tt class="xref docutils literal"><span class="pre">subscription</span></tt></a> | <a class="reference internal" href="#grammar-token-slicing"><tt class="xref docutils literal"><span class="pre">slicing</span></tt></a> | <a class="reference internal" href="#grammar-token-call"><tt class="xref docutils literal"><span class="pre">call</span></tt></a>
</pre>
<div class="section" id="attribute-references">
<span id="id6"></span><h3>5.3.1. Attribute references<a class="headerlink" href="#attribute-references" title="Permalink to this headline">¶</a></h3>
<p id="index-911">An attribute reference is a primary followed by a period and a name:</p>
<pre>
<strong id="grammar-token-attributeref">attributeref</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><tt class="xref docutils literal"><span class="pre">primary</span></tt></a> &quot;.&quot; <a class="reference external" href="lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a>
</pre>
<p id="index-912">The primary must evaluate to an object of a type that supports attribute
references, e.g., a module, list, or an instance.  This object is then asked to
produce the attribute whose name is the identifier.  If this attribute is not
available, the exception <a title="exceptions.AttributeError" class="reference external" href="../library/exceptions.html#exceptions.AttributeError"><tt class="xref docutils literal"><span class="pre">AttributeError</span></tt></a> is raised. Otherwise, the type
and value of the object produced is determined by the object.  Multiple
evaluations of the same attribute reference may yield different objects.</p>
</div>
<div class="section" id="subscriptions">
<span id="id7"></span><h3>5.3.2. Subscriptions<a class="headerlink" href="#subscriptions" title="Permalink to this headline">¶</a></h3>
<span class="target" id="index-913"></span><p id="index-914">A subscription selects an item of a sequence (string, tuple or list) or mapping
(dictionary) object:</p>
<pre>
<strong id="grammar-token-subscription">subscription</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><tt class="xref docutils literal"><span class="pre">primary</span></tt></a> &quot;[&quot; <a class="reference internal" href="#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a> &quot;]&quot;
</pre>
<p>The primary must evaluate to an object of a sequence or mapping type.</p>
<p>If the primary is a mapping, the expression list must evaluate to an object
whose value is one of the keys of the mapping, and the subscription selects the
value in the mapping that corresponds to that key.  (The expression list is a
tuple except if it has exactly one item.)</p>
<p>If the primary is a sequence, the expression (list) must evaluate to a plain
integer.  If this value is negative, the length of the sequence is added to it
(so that, e.g., <tt class="docutils literal"><span class="pre">x[-1]</span></tt> selects the last item of <tt class="docutils literal"><span class="pre">x</span></tt>.)  The resulting value
must be a nonnegative integer less than the number of items in the sequence, and
the subscription selects the item whose index is that value (counting from
zero).</p>
<p id="index-915">A string&#8217;s items are characters.  A character is not a separate data type but a
string of exactly one character.</p>
</div>
<div class="section" id="slicings">
<span id="id8"></span><h3>5.3.3. Slicings<a class="headerlink" href="#slicings" title="Permalink to this headline">¶</a></h3>
<span class="target" id="index-916"></span><p id="index-917">A slicing selects a range of items in a sequence object (e.g., a string, tuple
or list).  Slicings may be used as expressions or as targets in assignment or
<a class="reference external" href="simple_stmts.html#del"><tt class="xref docutils literal"><span class="pre">del</span></tt></a> statements.  The syntax for a slicing:</p>
<pre>
<strong id="grammar-token-slicing">slicing         </strong> ::=  <a class="reference internal" href="#grammar-token-simple_slicing"><tt class="xref docutils literal"><span class="pre">simple_slicing</span></tt></a> | <a class="reference internal" href="#grammar-token-extended_slicing"><tt class="xref docutils literal"><span class="pre">extended_slicing</span></tt></a>
<strong id="grammar-token-simple_slicing">simple_slicing  </strong> ::=  <a class="reference internal" href="#grammar-token-primary"><tt class="xref docutils literal"><span class="pre">primary</span></tt></a> &quot;[&quot; <a class="reference internal" href="#grammar-token-short_slice"><tt class="xref docutils literal"><span class="pre">short_slice</span></tt></a> &quot;]&quot;
<strong id="grammar-token-extended_slicing">extended_slicing</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><tt class="xref docutils literal"><span class="pre">primary</span></tt></a> &quot;[&quot; <a class="reference internal" href="#grammar-token-slice_list"><tt class="xref docutils literal"><span class="pre">slice_list</span></tt></a> &quot;]&quot;
<strong id="grammar-token-slice_list">slice_list      </strong> ::=  <a class="reference internal" href="#grammar-token-slice_item"><tt class="xref docutils literal"><span class="pre">slice_item</span></tt></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-slice_item"><tt class="xref docutils literal"><span class="pre">slice_item</span></tt></a>)* [&quot;,&quot;]
<strong id="grammar-token-slice_item">slice_item      </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> | <a class="reference internal" href="#grammar-token-proper_slice"><tt class="xref docutils literal"><span class="pre">proper_slice</span></tt></a> | <a class="reference internal" href="#grammar-token-ellipsis"><tt class="xref docutils literal"><span class="pre">ellipsis</span></tt></a>
<strong id="grammar-token-proper_slice">proper_slice    </strong> ::=  <a class="reference internal" href="#grammar-token-short_slice"><tt class="xref docutils literal"><span class="pre">short_slice</span></tt></a> | <a class="reference internal" href="#grammar-token-long_slice"><tt class="xref docutils literal"><span class="pre">long_slice</span></tt></a>
<strong id="grammar-token-short_slice">short_slice     </strong> ::=  [<a class="reference internal" href="#grammar-token-lower_bound"><tt class="xref docutils literal"><span class="pre">lower_bound</span></tt></a>] &quot;:&quot; [<a class="reference internal" href="#grammar-token-upper_bound"><tt class="xref docutils literal"><span class="pre">upper_bound</span></tt></a>]
<strong id="grammar-token-long_slice">long_slice      </strong> ::=  <a class="reference internal" href="#grammar-token-short_slice"><tt class="xref docutils literal"><span class="pre">short_slice</span></tt></a> &quot;:&quot; [<a class="reference internal" href="#grammar-token-stride"><tt class="xref docutils literal"><span class="pre">stride</span></tt></a>]
<strong id="grammar-token-lower_bound">lower_bound     </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>
<strong id="grammar-token-upper_bound">upper_bound     </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>
<strong id="grammar-token-stride">stride          </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>
<strong id="grammar-token-ellipsis">ellipsis        </strong> ::=  &quot;...&quot;
</pre>
<p id="index-918">There is ambiguity in the formal syntax here: anything that looks like an
expression list also looks like a slice list, so any subscription can be
interpreted as a slicing.  Rather than further complicating the syntax, this is
disambiguated by defining that in this case the interpretation as a subscription
takes priority over the interpretation as a slicing (this is the case if the
slice list contains no proper slice nor ellipses).  Similarly, when the slice
list has exactly one short slice and no trailing comma, the interpretation as a
simple slicing takes priority over that as an extended slicing.</p>
<p>The semantics for a simple slicing are as follows.  The primary must evaluate to
a sequence object.  The lower and upper bound expressions, if present, must
evaluate to plain integers; defaults are zero and the <tt class="docutils literal"><span class="pre">sys.maxint</span></tt>,
respectively.  If either bound is negative, the sequence&#8217;s length is added to
it.  The slicing now selects all items with index <em>k</em> such that <tt class="docutils literal"><span class="pre">i</span> <span class="pre">&lt;=</span> <span class="pre">k</span> <span class="pre">&lt;</span> <span class="pre">j</span></tt>
where <em>i</em> and <em>j</em> are the specified lower and upper bounds.  This may be an
empty sequence.  It is not an error if <em>i</em> or <em>j</em> lie outside the range of valid
indexes (such items don&#8217;t exist so they aren&#8217;t selected).</p>
<p id="index-919">The semantics for an extended slicing are as follows.  The primary must evaluate
to a mapping object, and it is indexed with a key that is constructed from the
slice list, as follows.  If the slice list contains at least one comma, the key
is a tuple containing the conversion of the slice items; otherwise, the
conversion of the lone slice item is the key.  The conversion of a slice item
that is an expression is that expression.  The conversion of an ellipsis slice
item is the built-in <tt class="docutils literal"><span class="pre">Ellipsis</span></tt> object.  The conversion of a proper slice is a
slice object (see section <a class="reference external" href="datamodel.html#types"><em>The standard type hierarchy</em></a>) whose <tt class="xref docutils literal"><span class="pre">start</span></tt>, <tt class="xref docutils literal"><span class="pre">stop</span></tt> and
<tt class="xref docutils literal"><span class="pre">step</span></tt> attributes are the values of the expressions given as lower bound,
upper bound and stride, respectively, substituting <tt class="xref docutils literal"><span class="pre">None</span></tt> for missing
expressions.</p>
</div>
<div class="section" id="calls">
<span id="id9"></span><h3>5.3.4. Calls<a class="headerlink" href="#calls" title="Permalink to this headline">¶</a></h3>
<span class="target" id="index-920"></span><p id="index-921">A call calls a callable object (e.g., a function) with a possibly empty series
of arguments:</p>
<pre>
<strong id="grammar-token-call">call                </strong> ::=  <a class="reference internal" href="#grammar-token-primary"><tt class="xref docutils literal"><span class="pre">primary</span></tt></a> &quot;(&quot; [<a class="reference internal" href="#grammar-token-argument_list"><tt class="xref docutils literal"><span class="pre">argument_list</span></tt></a> [&quot;,&quot;]
                          | <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> <a class="reference internal" href="#grammar-token-genexpr_for"><tt class="xref docutils literal"><span class="pre">genexpr_for</span></tt></a>] &quot;)&quot;
<strong id="grammar-token-argument_list">argument_list       </strong> ::=  <a class="reference internal" href="#grammar-token-positional_arguments"><tt class="xref docutils literal"><span class="pre">positional_arguments</span></tt></a> [&quot;,&quot; <a class="reference internal" href="#grammar-token-keyword_arguments"><tt class="xref docutils literal"><span class="pre">keyword_arguments</span></tt></a>]
                            [&quot;,&quot; &quot;*&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>] [&quot;,&quot; <a class="reference internal" href="#grammar-token-keyword_arguments"><tt class="xref docutils literal"><span class="pre">keyword_arguments</span></tt></a>]
                            [&quot;,&quot; &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>]
                          | <a class="reference internal" href="#grammar-token-keyword_arguments"><tt class="xref docutils literal"><span class="pre">keyword_arguments</span></tt></a> [&quot;,&quot; &quot;*&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>]
                            [&quot;,&quot; &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>]
                          | &quot;*&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> [&quot;,&quot; &quot;*&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>] [&quot;,&quot; &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>]
                          | &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>
<strong id="grammar-token-positional_arguments">positional_arguments</strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>)*
<strong id="grammar-token-keyword_arguments">keyword_arguments   </strong> ::=  <a class="reference internal" href="#grammar-token-keyword_item"><tt class="xref docutils literal"><span class="pre">keyword_item</span></tt></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-keyword_item"><tt class="xref docutils literal"><span class="pre">keyword_item</span></tt></a>)*
<strong id="grammar-token-keyword_item">keyword_item        </strong> ::=  <a class="reference external" href="lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a> &quot;=&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>
</pre>
<p>A trailing comma may be present after the positional and keyword arguments but
does not affect the semantics.</p>
<p>The primary must evaluate to a callable object (user-defined functions, built-in
functions, methods of built-in objects, class objects, methods of class
instances, and certain class instances themselves are callable; extensions may
define additional callable object types).  All argument expressions are
evaluated before the call is attempted.  Please refer to section <a class="reference external" href="compound_stmts.html#function"><em>Function definitions</em></a>
for the syntax of formal parameter lists.</p>
<p>If keyword arguments are present, they are first converted to positional
arguments, as follows.  First, a list of unfilled slots is created for the
formal parameters.  If there are N positional arguments, they are placed in the
first N slots.  Next, for each keyword argument, the identifier is used to
determine the corresponding slot (if the identifier is the same as the first
formal parameter name, the first slot is used, and so on).  If the slot is
already filled, a <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised. Otherwise, the value of
the argument is placed in the slot, filling it (even if the expression is
<tt class="xref docutils literal"><span class="pre">None</span></tt>, it fills the slot).  When all arguments have been processed, the slots
that are still unfilled are filled with the corresponding default value from the
function definition.  (Default values are calculated, once, when the function is
defined; thus, a mutable object such as a list or dictionary used as default
value will be shared by all calls that don&#8217;t specify an argument value for the
corresponding slot; this should usually be avoided.)  If there are any unfilled
slots for which no default value is specified, a <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> exception is
raised.  Otherwise, the list of filled slots is used as the argument list for
the call.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">An implementation may provide builtin functions whose positional parameters do
not have names, even if they are &#8216;named&#8217; for the purpose of documentation, and
which therefore cannot be supplied by keyword.  In CPython, this is the case for
functions implemented in C that use <a title="PyArg_ParseTuple" class="reference external" href="../c-api/arg.html#PyArg_ParseTuple"><tt class="xref docutils literal"><span class="pre">PyArg_ParseTuple</span></tt></a> to parse their
arguments.</p>
</div>
<p>If there are more positional arguments than there are formal parameter slots, a
<a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised, unless a formal parameter using the syntax
<tt class="docutils literal"><span class="pre">*identifier</span></tt> is present; in this case, that formal parameter receives a tuple
containing the excess positional arguments (or an empty tuple if there were no
excess positional arguments).</p>
<p>If any keyword argument does not correspond to a formal parameter name, a
<a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised, unless a formal parameter using the syntax
<tt class="docutils literal"><span class="pre">**identifier</span></tt> is present; in this case, that formal parameter receives a
dictionary containing the excess keyword arguments (using the keywords as keys
and the argument values as corresponding values), or a (new) empty dictionary if
there were no excess keyword arguments.</p>
<p>If the syntax <tt class="docutils literal"><span class="pre">*expression</span></tt> appears in the function call, <tt class="docutils literal"><span class="pre">expression</span></tt> must
evaluate to a sequence.  Elements from this sequence are treated as if they were
additional positional arguments; if there are positional arguments <em>x1</em>,...,
<em>xN</em>, and <tt class="docutils literal"><span class="pre">expression</span></tt> evaluates to a sequence <em>y1</em>, ..., <em>yM</em>, this is
equivalent to a call with M+N positional arguments <em>x1</em>, ..., <em>xN</em>, <em>y1</em>, ...,
<em>yM</em>.</p>
<p>A consequence of this is that although the <tt class="docutils literal"><span class="pre">*expression</span></tt> syntax may appear
<em>after</em> some keyword arguments, it is processed <em>before</em> the keyword arguments
(and the <tt class="docutils literal"><span class="pre">**expression</span></tt> argument, if any &#8211; see below).  So:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">b</span><span class="o">=</span><span class="mf">1</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mf">2</span><span class="p">,))</span>
<span class="go">2 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mf">1</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mf">2</span><span class="p">,))</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
<span class="nc">TypeError</span>: <span class="n-Identifier">f() got multiple values for keyword argument &#39;a&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mf">2</span><span class="p">,))</span>
<span class="go">1 2</span>
</pre></div>
</div>
<p>It is unusual for both keyword arguments and the <tt class="docutils literal"><span class="pre">*expression</span></tt> syntax to be
used in the same call, so in practice this confusion does not arise.</p>
<p>If the syntax <tt class="docutils literal"><span class="pre">**expression</span></tt> appears in the function call, <tt class="docutils literal"><span class="pre">expression</span></tt> must
evaluate to a mapping, the contents of which are treated as additional keyword
arguments.  In the case of a keyword appearing in both <tt class="docutils literal"><span class="pre">expression</span></tt> and as an
explicit keyword argument, a <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised.</p>
<p>Formal parameters using the syntax <tt class="docutils literal"><span class="pre">*identifier</span></tt> or <tt class="docutils literal"><span class="pre">**identifier</span></tt> cannot be
used as positional argument slots or as keyword argument names.  Formal
parameters using the syntax <tt class="docutils literal"><span class="pre">(sublist)</span></tt> cannot be used as keyword argument
names; the outermost sublist corresponds to a single unnamed argument slot, and
the argument value is assigned to the sublist using the usual tuple assignment
rules after all other parameter processing is done.</p>
<p>A call always returns some value, possibly <tt class="xref docutils literal"><span class="pre">None</span></tt>, unless it raises an
exception.  How this value is computed depends on the type of the callable
object.</p>
<p>If it is&#8212;</p>
<dl class="docutils">
<dt>a user-defined function:</dt>
<dd><p class="first last" id="index-922">The code block for the function is executed, passing it the argument list.  The
first thing the code block will do is bind the formal parameters to the
arguments; this is described in section <a class="reference external" href="compound_stmts.html#function"><em>Function definitions</em></a>.  When the code block
executes a <a class="reference external" href="simple_stmts.html#return"><tt class="xref docutils literal"><span class="pre">return</span></tt></a> statement, this specifies the return value of the
function call.</p>
</dd>
<dt>a built-in function or method:</dt>
<dd><p class="first last" id="index-923">The result is up to the interpreter; see <a class="reference external" href="../library/functions.html#built-in-funcs"><em>Built-in Functions</em></a> for the
descriptions of built-in functions and methods.</p>
</dd>
<dt>a class object:</dt>
<dd><p class="first last" id="index-924">A new instance of that class is returned.</p>
</dd>
<dt>a class instance method:</dt>
<dd><p class="first last" id="index-925">The corresponding user-defined function is called, with an argument list that is
one longer than the argument list of the call: the instance becomes the first
argument.</p>
</dd>
<dt>a class instance:</dt>
<dd><p class="first last" id="index-926">The class must define a <a title="object.__call__" class="reference external" href="datamodel.html#object.__call__"><tt class="xref docutils literal"><span class="pre">__call__()</span></tt></a> method; the effect is then the same as
if that method was called.</p>
</dd>
</dl>
</div>
</div>
<div class="section" id="the-power-operator">
<span id="power"></span><h2>5.4. The power operator<a class="headerlink" href="#the-power-operator" title="Permalink to this headline">¶</a></h2>
<p>The power operator binds more tightly than unary operators on its left; it binds
less tightly than unary operators on its right.  The syntax is:</p>
<pre>
<strong id="grammar-token-power">power</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><tt class="xref docutils literal"><span class="pre">primary</span></tt></a> [&quot;**&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a>]
</pre>
<p>Thus, in an unparenthesized sequence of power and unary operators, the operators
are evaluated from right to left (this does not constrain the evaluation order
for the operands): <tt class="docutils literal"><span class="pre">-1**2</span></tt> results in <tt class="docutils literal"><span class="pre">-1</span></tt>.</p>
<p>The power operator has the same semantics as the built-in <a title="pow" class="reference external" href="../library/functions.html#pow"><tt class="xref docutils literal"><span class="pre">pow()</span></tt></a> function,
when called with two arguments: it yields its left argument raised to the power
of its right argument.  The numeric arguments are first converted to a common
type.  The result type is that of the arguments after coercion.</p>
<p>With mixed operand types, the coercion rules for binary arithmetic operators
apply. For int and long int operands, the result has the same type as the
operands (after coercion) unless the second argument is negative; in that case,
all arguments are converted to float and a float result is delivered. For
example, <tt class="docutils literal"><span class="pre">10**2</span></tt> returns <tt class="docutils literal"><span class="pre">100</span></tt>, but <tt class="docutils literal"><span class="pre">10**-2</span></tt> returns <tt class="docutils literal"><span class="pre">0.01</span></tt>. (This last
feature was added in Python 2.2. In Python 2.1 and before, if both arguments
were of integer types and the second argument was negative, an exception was
raised).</p>
<p>Raising <tt class="docutils literal"><span class="pre">0.0</span></tt> to a negative power results in a <a title="exceptions.ZeroDivisionError" class="reference external" href="../library/exceptions.html#exceptions.ZeroDivisionError"><tt class="xref docutils literal"><span class="pre">ZeroDivisionError</span></tt></a>.
Raising a negative number to a fractional power results in a <a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a>.</p>
</div>
<div class="section" id="unary-arithmetic-and-bitwise-operations">
<span id="unary"></span><h2>5.5. Unary arithmetic and bitwise operations<a class="headerlink" href="#unary-arithmetic-and-bitwise-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-927">All unary arithmetic and bitwise operations have the same priority:</p>
<pre>
<strong id="grammar-token-u_expr">u_expr</strong> ::=  <a class="reference internal" href="#grammar-token-power"><tt class="xref docutils literal"><span class="pre">power</span></tt></a> | &quot;-&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a> | &quot;+&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a> | &quot;~&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a>
</pre>
<p id="index-928">The unary <tt class="docutils literal"><span class="pre">-</span></tt> (minus) operator yields the negation of its numeric argument.</p>
<p id="index-929">The unary <tt class="docutils literal"><span class="pre">+</span></tt> (plus) operator yields its numeric argument unchanged.</p>
<p id="index-930">The unary <tt class="docutils literal"><span class="pre">~</span></tt> (invert) operator yields the bitwise inversion of its plain or
long integer argument.  The bitwise inversion of <tt class="docutils literal"><span class="pre">x</span></tt> is defined as
<tt class="docutils literal"><span class="pre">-(x+1)</span></tt>.  It only applies to integral numbers.</p>
<p id="index-931">In all three cases, if the argument does not have the proper type, a
<a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised.</p>
</div>
<div class="section" id="binary-arithmetic-operations">
<span id="binary"></span><h2>5.6. Binary arithmetic operations<a class="headerlink" href="#binary-arithmetic-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-932">The binary arithmetic operations have the conventional priority levels.  Note
that some of these operations also apply to certain non-numeric types.  Apart
from the power operator, there are only two levels, one for multiplicative
operators and one for additive operators:</p>
<pre>
<strong id="grammar-token-m_expr">m_expr</strong> ::=  <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-m_expr"><tt class="xref docutils literal"><span class="pre">m_expr</span></tt></a> &quot;*&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-m_expr"><tt class="xref docutils literal"><span class="pre">m_expr</span></tt></a> &quot;//&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-m_expr"><tt class="xref docutils literal"><span class="pre">m_expr</span></tt></a> &quot;/&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a>
            | <a class="reference internal" href="#grammar-token-m_expr"><tt class="xref docutils literal"><span class="pre">m_expr</span></tt></a> &quot;%&quot; <a class="reference internal" href="#grammar-token-u_expr"><tt class="xref docutils literal"><span class="pre">u_expr</span></tt></a>
<strong id="grammar-token-a_expr">a_expr</strong> ::=  <a class="reference internal" href="#grammar-token-m_expr"><tt class="xref docutils literal"><span class="pre">m_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-a_expr"><tt class="xref docutils literal"><span class="pre">a_expr</span></tt></a> &quot;+&quot; <a class="reference internal" href="#grammar-token-m_expr"><tt class="xref docutils literal"><span class="pre">m_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-a_expr"><tt class="xref docutils literal"><span class="pre">a_expr</span></tt></a> &quot;-&quot; <a class="reference internal" href="#grammar-token-m_expr"><tt class="xref docutils literal"><span class="pre">m_expr</span></tt></a>
</pre>
<p id="index-933">The <tt class="docutils literal"><span class="pre">*</span></tt> (multiplication) operator yields the product of its arguments.  The
arguments must either both be numbers, or one argument must be an integer (plain
or long) and the other must be a sequence. In the former case, the numbers are
converted to a common type and then multiplied together.  In the latter case,
sequence repetition is performed; a negative repetition factor yields an empty
sequence.</p>
<p id="index-934">The <tt class="docutils literal"><span class="pre">/</span></tt> (division) and <tt class="docutils literal"><span class="pre">//</span></tt> (floor division) operators yield the quotient of
their arguments.  The numeric arguments are first converted to a common type.
Plain or long integer division yields an integer of the same type; the result is
that of mathematical division with the &#8216;floor&#8217; function applied to the result.
Division by zero raises the <a title="exceptions.ZeroDivisionError" class="reference external" href="../library/exceptions.html#exceptions.ZeroDivisionError"><tt class="xref docutils literal"><span class="pre">ZeroDivisionError</span></tt></a> exception.</p>
<p id="index-935">The <tt class="docutils literal"><span class="pre">%</span></tt> (modulo) operator yields the remainder from the division of the first
argument by the second.  The numeric arguments are first converted to a common
type.  A zero right argument raises the <a title="exceptions.ZeroDivisionError" class="reference external" href="../library/exceptions.html#exceptions.ZeroDivisionError"><tt class="xref docutils literal"><span class="pre">ZeroDivisionError</span></tt></a> exception.  The
arguments may be floating point numbers, e.g., <tt class="docutils literal"><span class="pre">3.14%0.7</span></tt> equals <tt class="docutils literal"><span class="pre">0.34</span></tt>
(since <tt class="docutils literal"><span class="pre">3.14</span></tt> equals <tt class="docutils literal"><span class="pre">4*0.7</span> <span class="pre">+</span> <span class="pre">0.34</span></tt>.)  The modulo operator always yields a
result with the same sign as its second operand (or zero); the absolute value of
the result is strictly smaller than the absolute value of the second operand
<a class="footnote-reference" href="#id20" id="id10">[2]</a>.</p>
<p>The integer division and modulo operators are connected by the following
identity: <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">(x/y)*y</span> <span class="pre">+</span> <span class="pre">(x%y)</span></tt>.  Integer division and modulo are also
connected with the built-in function <a title="divmod" class="reference external" href="../library/functions.html#divmod"><tt class="xref docutils literal"><span class="pre">divmod()</span></tt></a>: <tt class="docutils literal"><span class="pre">divmod(x,</span> <span class="pre">y)</span> <span class="pre">==</span> <span class="pre">(x/y,</span>
<span class="pre">x%y)</span></tt>.  These identities don&#8217;t hold for floating point numbers; there similar
identities hold approximately where <tt class="docutils literal"><span class="pre">x/y</span></tt> is replaced by <tt class="docutils literal"><span class="pre">floor(x/y)</span></tt> or
<tt class="docutils literal"><span class="pre">floor(x/y)</span> <span class="pre">-</span> <span class="pre">1</span></tt> <a class="footnote-reference" href="#id21" id="id11">[3]</a>.</p>
<p>In addition to performing the modulo operation on numbers, the <tt class="docutils literal"><span class="pre">%</span></tt> operator is
also overloaded by string and unicode objects to perform string formatting (also
known as interpolation). The syntax for string formatting is described in the
Python Library Reference, section <a class="reference external" href="../library/stdtypes.html#string-formatting"><em>String Formatting Operations</em></a>.</p>
<p>
<span class="versionmodified">Deprecated since version 2.3: </span>The floor division operator, the modulo operator, and the <a title="divmod" class="reference external" href="../library/functions.html#divmod"><tt class="xref docutils literal"><span class="pre">divmod()</span></tt></a>
function are no longer defined for complex numbers.  Instead, convert to a
floating point number using the <a title="abs" class="reference external" href="../library/functions.html#abs"><tt class="xref docutils literal"><span class="pre">abs()</span></tt></a> function if appropriate.</p>
<p id="index-936">The <tt class="docutils literal"><span class="pre">+</span></tt> (addition) operator yields the sum of its arguments. The arguments
must either both be numbers or both sequences of the same type.  In the former
case, the numbers are converted to a common type and then added together.  In
the latter case, the sequences are concatenated.</p>
<p id="index-937">The <tt class="docutils literal"><span class="pre">-</span></tt> (subtraction) operator yields the difference of its arguments.  The
numeric arguments are first converted to a common type.</p>
</div>
<div class="section" id="shifting-operations">
<span id="shifting"></span><h2>5.7. Shifting operations<a class="headerlink" href="#shifting-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-938">The shifting operations have lower priority than the arithmetic operations:</p>
<pre>
<strong id="grammar-token-shift_expr">shift_expr</strong> ::=  <a class="reference internal" href="#grammar-token-a_expr"><tt class="xref docutils literal"><span class="pre">a_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-shift_expr"><tt class="xref docutils literal"><span class="pre">shift_expr</span></tt></a> ( &quot;&lt;&lt;&quot; | &quot;&gt;&gt;&quot; ) <a class="reference internal" href="#grammar-token-a_expr"><tt class="xref docutils literal"><span class="pre">a_expr</span></tt></a>
</pre>
<p>These operators accept plain or long integers as arguments.  The arguments are
converted to a common type.  They shift the first argument to the left or right
by the number of bits given by the second argument.</p>
<p id="index-939">A right shift by <em>n</em> bits is defined as division by <tt class="docutils literal"><span class="pre">pow(2,</span> <span class="pre">n)</span></tt>.  A left shift
by <em>n</em> bits is defined as multiplication with <tt class="docutils literal"><span class="pre">pow(2,</span> <span class="pre">n)</span></tt>.  Negative shift
counts raise a <a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> exception.</p>
</div>
<div class="section" id="binary-bitwise-operations">
<span id="bitwise"></span><h2>5.8. Binary bitwise operations<a class="headerlink" href="#binary-bitwise-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-940">Each of the three bitwise operations has a different priority level:</p>
<pre>
<strong id="grammar-token-and_expr">and_expr</strong> ::=  <a class="reference internal" href="#grammar-token-shift_expr"><tt class="xref docutils literal"><span class="pre">shift_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-and_expr"><tt class="xref docutils literal"><span class="pre">and_expr</span></tt></a> &quot;&amp;&quot; <a class="reference internal" href="#grammar-token-shift_expr"><tt class="xref docutils literal"><span class="pre">shift_expr</span></tt></a>
<strong id="grammar-token-xor_expr">xor_expr</strong> ::=  <a class="reference internal" href="#grammar-token-and_expr"><tt class="xref docutils literal"><span class="pre">and_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-xor_expr"><tt class="xref docutils literal"><span class="pre">xor_expr</span></tt></a> &quot;^&quot; <a class="reference internal" href="#grammar-token-and_expr"><tt class="xref docutils literal"><span class="pre">and_expr</span></tt></a>
<strong id="grammar-token-or_expr">or_expr </strong> ::=  <a class="reference internal" href="#grammar-token-xor_expr"><tt class="xref docutils literal"><span class="pre">xor_expr</span></tt></a> | <a class="reference internal" href="#grammar-token-or_expr"><tt class="xref docutils literal"><span class="pre">or_expr</span></tt></a> &quot;|&quot; <a class="reference internal" href="#grammar-token-xor_expr"><tt class="xref docutils literal"><span class="pre">xor_expr</span></tt></a>
</pre>
<p id="index-941">The <tt class="docutils literal"><span class="pre">&amp;</span></tt> operator yields the bitwise AND of its arguments, which must be plain
or long integers.  The arguments are converted to a common type.</p>
<p id="index-942">The <tt class="docutils literal"><span class="pre">^</span></tt> operator yields the bitwise XOR (exclusive OR) of its arguments, which
must be plain or long integers.  The arguments are converted to a common type.</p>
<p id="index-943">The <tt class="docutils literal"><span class="pre">|</span></tt> operator yields the bitwise (inclusive) OR of its arguments, which
must be plain or long integers.  The arguments are converted to a common type.</p>
</div>
<div class="section" id="notin">
<span id="in"></span><span id="isnot"></span><span id="is"></span><span id="comparisons"></span><span id="id12"></span><h2>5.9. Comparisons<a class="headerlink" href="#notin" title="Permalink to this headline">¶</a></h2>
<span class="target" id="index-944"></span><p id="index-945">Unlike C, all comparison operations in Python have the same priority, which is
lower than that of any arithmetic, shifting or bitwise operation.  Also unlike
C, expressions like <tt class="docutils literal"><span class="pre">a</span> <span class="pre">&lt;</span> <span class="pre">b</span> <span class="pre">&lt;</span> <span class="pre">c</span></tt> have the interpretation that is conventional
in mathematics:</p>
<pre>
<strong id="grammar-token-comparison">comparison   </strong> ::=  <a class="reference internal" href="#grammar-token-or_expr"><tt class="xref docutils literal"><span class="pre">or_expr</span></tt></a> ( <a class="reference internal" href="#grammar-token-comp_operator"><tt class="xref docutils literal"><span class="pre">comp_operator</span></tt></a> <a class="reference internal" href="#grammar-token-or_expr"><tt class="xref docutils literal"><span class="pre">or_expr</span></tt></a> )*
<strong id="grammar-token-comp_operator">comp_operator</strong> ::=  &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;==&quot; | &quot;&gt;=&quot; | &quot;&lt;=&quot; | &quot;&lt;&gt;&quot; | &quot;!=&quot;
                   | &quot;is&quot; [&quot;not&quot;] | [&quot;not&quot;] &quot;in&quot;
</pre>
<p>Comparisons yield boolean values: <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>.</p>
<p id="index-946">Comparisons can be chained arbitrarily, e.g., <tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">&lt;=</span> <span class="pre">z</span></tt> is equivalent to
<tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">and</span> <span class="pre">y</span> <span class="pre">&lt;=</span> <span class="pre">z</span></tt>, except that <tt class="docutils literal"><span class="pre">y</span></tt> is evaluated only once (but in both
cases <tt class="docutils literal"><span class="pre">z</span></tt> is not evaluated at all when <tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span></tt> is found to be false).</p>
<p>Formally, if <em>a</em>, <em>b</em>, <em>c</em>, ..., <em>y</em>, <em>z</em> are expressions and <em>op1</em>, <em>op2</em>, ...,
<em>opN</em> are comparison operators, then <tt class="docutils literal"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span> <span class="pre">...</span> <span class="pre">y</span> <span class="pre">opN</span> <span class="pre">z</span></tt> is equivalent
to <tt class="docutils literal"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">and</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span> <span class="pre">and</span> <span class="pre">...</span> <span class="pre">y</span> <span class="pre">opN</span> <span class="pre">z</span></tt>, except that each expression is
evaluated at most once.</p>
<p>Note that <tt class="docutils literal"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span></tt> doesn&#8217;t imply any kind of comparison between <em>a</em> and
<em>c</em>, so that, e.g., <tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">&gt;</span> <span class="pre">z</span></tt> is perfectly legal (though perhaps not
pretty).</p>
<p>The forms <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt> and <tt class="docutils literal"><span class="pre">!=</span></tt> are equivalent; for consistency with C, <tt class="docutils literal"><span class="pre">!=</span></tt> is
preferred; where <tt class="docutils literal"><span class="pre">!=</span></tt> is mentioned below <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt> is also accepted.  The <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt>
spelling is considered obsolescent.</p>
<p>The operators <tt class="docutils literal"><span class="pre">&lt;</span></tt>, <tt class="docutils literal"><span class="pre">&gt;</span></tt>, <tt class="docutils literal"><span class="pre">==</span></tt>, <tt class="docutils literal"><span class="pre">&gt;=</span></tt>, <tt class="docutils literal"><span class="pre">&lt;=</span></tt>, and <tt class="docutils literal"><span class="pre">!=</span></tt> compare the
values of two objects.  The objects need not have the same type. If both are
numbers, they are converted to a common type.  Otherwise, objects of different
types <em>always</em> compare unequal, and are ordered consistently but arbitrarily.
You can control comparison behavior of objects of non-builtin types by defining
a <tt class="docutils literal"><span class="pre">__cmp__</span></tt> method or rich comparison methods like <tt class="docutils literal"><span class="pre">__gt__</span></tt>, described in
section <a class="reference external" href="datamodel.html#specialnames"><em>Special method names</em></a>.</p>
<p>(This unusual definition of comparison was used to simplify the definition of
operations like sorting and the <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> and <a class="reference internal" href="#notin"><tt class="xref docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt></a> operators.
In the future, the comparison rules for objects of different types are likely to
change.)</p>
<p>Comparison of objects of the same type depends on the type:</p>
<ul>
<li><p class="first">Numbers are compared arithmetically.</p>
</li>
<li><p class="first">Strings are compared lexicographically using the numeric equivalents (the
result of the built-in function <a title="ord" class="reference external" href="../library/functions.html#ord"><tt class="xref docutils literal"><span class="pre">ord()</span></tt></a>) of their characters.  Unicode and
8-bit strings are fully interoperable in this behavior. <a class="footnote-reference" href="#id22" id="id13">[4]</a></p>
</li>
<li><p class="first">Tuples and lists are compared lexicographically using comparison of
corresponding elements.  This means that to compare equal, each element must
compare equal and the two sequences must be of the same type and have the same
length.</p>
<p>If not equal, the sequences are ordered the same as their first differing
elements.  For example, <tt class="docutils literal"><span class="pre">cmp([1,2,x],</span> <span class="pre">[1,2,y])</span></tt> returns the same as
<tt class="docutils literal"><span class="pre">cmp(x,y)</span></tt>.  If the corresponding element does not exist, the shorter sequence
is ordered first (for example, <tt class="docutils literal"><span class="pre">[1,2]</span> <span class="pre">&lt;</span> <span class="pre">[1,2,3]</span></tt>).</p>
</li>
<li><p class="first">Mappings (dictionaries) compare equal if and only if their sorted (key, value)
lists compare equal. <a class="footnote-reference" href="#id23" id="id14">[5]</a> Outcomes other than equality are resolved
consistently, but are not otherwise defined. <a class="footnote-reference" href="#id24" id="id15">[6]</a></p>
</li>
<li><p class="first">Most other objects of builtin types compare unequal unless they are the same
object; the choice whether one object is considered smaller or larger than
another one is made arbitrarily but consistently within one execution of a
program.</p>
</li>
</ul>
<p>The operators <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> and <a class="reference internal" href="#notin"><tt class="xref docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt></a> test for collection
membership.  <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></tt> evaluates to true if <em>x</em> is a member of the collection
<em>s</em>, and false otherwise.  <tt class="docutils literal"><span class="pre">x</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">s</span></tt> returns the negation of <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></tt>.
The collection membership test has traditionally been bound to sequences; an
object is a member of a collection if the collection is a sequence and contains
an element equal to that object.  However, it make sense for many other object
types to support membership tests without being a sequence.  In particular,
dictionaries (for keys) and sets support membership testing.</p>
<p>For the list and tuple types, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> is true if and only if there exists an
index <em>i</em> such that <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y[i]</span></tt> is true.</p>
<p>For the Unicode and string types, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> is true if and only if <em>x</em> is a
substring of <em>y</em>.  An equivalent test is <tt class="docutils literal"><span class="pre">y.find(x)</span> <span class="pre">!=</span> <span class="pre">-1</span></tt>.  Note, <em>x</em> and <em>y</em>
need not be the same type; consequently, <tt class="docutils literal"><span class="pre">u'ab'</span> <span class="pre">in</span> <span class="pre">'abc'</span></tt> will return
<tt class="xref docutils literal"><span class="pre">True</span></tt>. Empty strings are always considered to be a substring of any other
string, so <tt class="docutils literal"><span class="pre">&quot;&quot;</span> <span class="pre">in</span> <span class="pre">&quot;abc&quot;</span></tt> will return <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
<p>
<span class="versionmodified">Changed in version 2.3: </span>Previously, <em>x</em> was required to be a string of length <tt class="docutils literal"><span class="pre">1</span></tt>.</p>
<p>For user-defined classes which define the <a title="object.__contains__" class="reference external" href="datamodel.html#object.__contains__"><tt class="xref docutils literal"><span class="pre">__contains__()</span></tt></a> method, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span>
<span class="pre">y</span></tt> is true if and only if <tt class="docutils literal"><span class="pre">y.__contains__(x)</span></tt> is true.</p>
<p>For user-defined classes which do not define <a title="object.__contains__" class="reference external" href="datamodel.html#object.__contains__"><tt class="xref docutils literal"><span class="pre">__contains__()</span></tt></a> and do define
<a title="object.__getitem__" class="reference external" href="datamodel.html#object.__getitem__"><tt class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a>, <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> is true if and only if there is a non-negative
integer index <em>i</em> such that <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y[i]</span></tt>, and all lower integer indices do not
raise <a title="exceptions.IndexError" class="reference external" href="../library/exceptions.html#exceptions.IndexError"><tt class="xref docutils literal"><span class="pre">IndexError</span></tt></a> exception. (If any other exception is raised, it is as
if <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> raised that exception).</p>
<p id="index-947">The operator <a class="reference internal" href="#notin"><tt class="xref docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt></a> is defined to have the inverse true value of
<a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a>.</p>
<p id="index-948">The operators <a class="reference internal" href="#is"><tt class="xref docutils literal"><span class="pre">is</span></tt></a> and <a class="reference internal" href="#isnot"><tt class="xref docutils literal"><span class="pre">is</span> <span class="pre">not</span></tt></a> test for object identity: <tt class="docutils literal"><span class="pre">x</span>
<span class="pre">is</span> <span class="pre">y</span></tt> is true if and only if <em>x</em> and <em>y</em> are the same object.  <tt class="docutils literal"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">y</span></tt>
yields the inverse truth value. <a class="footnote-reference" href="#id25" id="id16">[7]</a></p>
</div>
<div class="section" id="boolean-operations">
<span id="not"></span><span id="or"></span><span id="and"></span><span id="booleans"></span><h2>5.10. Boolean operations<a class="headerlink" href="#boolean-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-949">Boolean operations have the lowest priority of all Python operations:</p>
<pre>
<strong id="grammar-token-expression">expression            </strong> ::=  <a class="reference internal" href="#grammar-token-conditional_expression"><tt class="xref docutils literal"><span class="pre">conditional_expression</span></tt></a> | <a class="reference internal" href="#grammar-token-lambda_form"><tt class="xref docutils literal"><span class="pre">lambda_form</span></tt></a>
<strong id="grammar-token-old_expression">old_expression        </strong> ::=  <a class="reference internal" href="#grammar-token-or_test"><tt class="xref docutils literal"><span class="pre">or_test</span></tt></a> | <a class="reference internal" href="#grammar-token-old_lambda_form"><tt class="xref docutils literal"><span class="pre">old_lambda_form</span></tt></a>
<strong id="grammar-token-conditional_expression">conditional_expression</strong> ::=  <a class="reference internal" href="#grammar-token-or_test"><tt class="xref docutils literal"><span class="pre">or_test</span></tt></a> [&quot;if&quot; <a class="reference internal" href="#grammar-token-or_test"><tt class="xref docutils literal"><span class="pre">or_test</span></tt></a> &quot;else&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>]
<strong id="grammar-token-or_test">or_test               </strong> ::=  <a class="reference internal" href="#grammar-token-and_test"><tt class="xref docutils literal"><span class="pre">and_test</span></tt></a> | <a class="reference internal" href="#grammar-token-or_test"><tt class="xref docutils literal"><span class="pre">or_test</span></tt></a> &quot;or&quot; <a class="reference internal" href="#grammar-token-and_test"><tt class="xref docutils literal"><span class="pre">and_test</span></tt></a>
<strong id="grammar-token-and_test">and_test              </strong> ::=  <a class="reference internal" href="#grammar-token-not_test"><tt class="xref docutils literal"><span class="pre">not_test</span></tt></a> | <a class="reference internal" href="#grammar-token-and_test"><tt class="xref docutils literal"><span class="pre">and_test</span></tt></a> &quot;and&quot; <a class="reference internal" href="#grammar-token-not_test"><tt class="xref docutils literal"><span class="pre">not_test</span></tt></a>
<strong id="grammar-token-not_test">not_test              </strong> ::=  <a class="reference internal" href="#grammar-token-comparison"><tt class="xref docutils literal"><span class="pre">comparison</span></tt></a> | &quot;not&quot; <a class="reference internal" href="#grammar-token-not_test"><tt class="xref docutils literal"><span class="pre">not_test</span></tt></a>
</pre>
<p>In the context of Boolean operations, and also when expressions are used by
control flow statements, the following values are interpreted as false:
<tt class="xref docutils literal"><span class="pre">False</span></tt>, <tt class="xref docutils literal"><span class="pre">None</span></tt>, numeric zero of all types, and empty strings and containers
(including strings, tuples, lists, dictionaries, sets and frozensets).  All
other values are interpreted as true.  (See the <a title="object.__nonzero__" class="reference external" href="datamodel.html#object.__nonzero__"><tt class="xref docutils literal"><span class="pre">__nonzero__()</span></tt></a>
special method for a way to change this.)</p>
<p id="index-950">The operator <a class="reference internal" href="#not"><tt class="xref docutils literal"><span class="pre">not</span></tt></a> yields <tt class="xref docutils literal"><span class="pre">True</span></tt> if its argument is false, <tt class="xref docutils literal"><span class="pre">False</span></tt>
otherwise.</p>
<p>The expression <tt class="docutils literal"><span class="pre">x</span> <span class="pre">if</span> <span class="pre">C</span> <span class="pre">else</span> <span class="pre">y</span></tt> first evaluates <em>C</em> (<em>not</em> <em>x</em>); if <em>C</em> is
true, <em>x</em> is evaluated and its value is returned; otherwise, <em>y</em> is evaluated
and its value is returned.</p>
<p>
<span class="versionmodified">New in version 2.5.</span></p>
<p id="index-951">The expression <tt class="docutils literal"><span class="pre">x</span> <span class="pre">and</span> <span class="pre">y</span></tt> first evaluates <em>x</em>; if <em>x</em> is false, its value is
returned; otherwise, <em>y</em> is evaluated and the resulting value is returned.</p>
<p id="index-952">The expression <tt class="docutils literal"><span class="pre">x</span> <span class="pre">or</span> <span class="pre">y</span></tt> first evaluates <em>x</em>; if <em>x</em> is true, its value is
returned; otherwise, <em>y</em> is evaluated and the resulting value is returned.</p>
<p>(Note that neither <a class="reference internal" href="#and"><tt class="xref docutils literal"><span class="pre">and</span></tt></a> nor <a class="reference internal" href="#or"><tt class="xref docutils literal"><span class="pre">or</span></tt></a> restrict the value and type
they return to <tt class="xref docutils literal"><span class="pre">False</span></tt> and <tt class="xref docutils literal"><span class="pre">True</span></tt>, but rather return the last evaluated
argument. This is sometimes useful, e.g., if <tt class="docutils literal"><span class="pre">s</span></tt> is a string that should be
replaced by a default value if it is empty, the expression <tt class="docutils literal"><span class="pre">s</span> <span class="pre">or</span> <span class="pre">'foo'</span></tt> yields
the desired value.  Because <a class="reference internal" href="#not"><tt class="xref docutils literal"><span class="pre">not</span></tt></a> has to invent a value anyway, it does
not bother to return a value of the same type as its argument, so e.g., <tt class="docutils literal"><span class="pre">not</span>
<span class="pre">'foo'</span></tt> yields <tt class="xref docutils literal"><span class="pre">False</span></tt>, not <tt class="docutils literal"><span class="pre">''</span></tt>.)</p>
</div>
<div class="section" id="lambdas">
<span id="id17"></span><h2>5.11. Lambdas<a class="headerlink" href="#lambdas" title="Permalink to this headline">¶</a></h2>
<pre id="index-953">
<strong id="grammar-token-lambda_form">lambda_form    </strong> ::=  &quot;lambda&quot; [<a class="reference external" href="compound_stmts.html#grammar-token-parameter_list"><tt class="xref docutils literal"><span class="pre">parameter_list</span></tt></a>]: <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a>
<strong id="grammar-token-old_lambda_form">old_lambda_form</strong> ::=  &quot;lambda&quot; [<a class="reference external" href="compound_stmts.html#grammar-token-parameter_list"><tt class="xref docutils literal"><span class="pre">parameter_list</span></tt></a>]: <a class="reference internal" href="#grammar-token-old_expression"><tt class="xref docutils literal"><span class="pre">old_expression</span></tt></a>
</pre>
<p>Lambda forms (lambda expressions) have the same syntactic position as
expressions.  They are a shorthand to create anonymous functions; the expression
<tt class="docutils literal"><span class="pre">lambda</span> <span class="pre">arguments:</span> <span class="pre">expression</span></tt> yields a function object.  The unnamed object
behaves like a function object defined with</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="n">arguments</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">expression</span>
</pre></div>
</div>
<p>See section <a class="reference external" href="compound_stmts.html#function"><em>Function definitions</em></a> for the syntax of parameter lists.  Note that
functions created with lambda forms cannot contain statements.</p>
</div>
<div class="section" id="expression-lists">
<span id="exprlists"></span><span id="lambda"></span><h2>5.12. Expression lists<a class="headerlink" href="#expression-lists" title="Permalink to this headline">¶</a></h2>
<pre id="index-954">
<strong id="grammar-token-expression_list">expression_list</strong> ::=  <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> ( &quot;,&quot; <a class="reference internal" href="#grammar-token-expression"><tt class="xref docutils literal"><span class="pre">expression</span></tt></a> )* [&quot;,&quot;]
</pre>
<p id="index-955">An expression list containing at least one comma yields a tuple.  The length of
the tuple is the number of expressions in the list.  The expressions are
evaluated from left to right.</p>
<p id="index-956">The trailing comma is required only to create a single tuple (a.k.a. a
<em>singleton</em>); it is optional in all other cases.  A single expression without a
trailing comma doesn&#8217;t create a tuple, but rather yields the value of that
expression. (To create an empty tuple, use an empty pair of parentheses:
<tt class="docutils literal"><span class="pre">()</span></tt>.)</p>
</div>
<div class="section" id="evaluation-order">
<span id="evalorder"></span><h2>5.13. Evaluation order<a class="headerlink" href="#evaluation-order" title="Permalink to this headline">¶</a></h2>
<p id="index-957">Python evaluates expressions from left to right. Notice that while evaluating an
assignment, the right-hand side is evaluated before the left-hand side.</p>
<p>In the following lines, expressions will be evaluated in the arithmetic order of
their suffixes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">expr1</span><span class="p">,</span> <span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">,</span> <span class="n">expr4</span>
<span class="p">(</span><span class="n">expr1</span><span class="p">,</span> <span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">,</span> <span class="n">expr4</span><span class="p">)</span>
<span class="p">{</span><span class="n">expr1</span><span class="p">:</span> <span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">:</span> <span class="n">expr4</span><span class="p">}</span>
<span class="n">expr1</span> <span class="o">+</span> <span class="n">expr2</span> <span class="o">*</span> <span class="p">(</span><span class="n">expr3</span> <span class="o">-</span> <span class="n">expr4</span><span class="p">)</span>
<span class="n">expr1</span><span class="p">(</span><span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">,</span> <span class="o">*</span><span class="n">expr4</span><span class="p">,</span> <span class="o">**</span><span class="n">expr5</span><span class="p">)</span>
<span class="n">expr3</span><span class="p">,</span> <span class="n">expr4</span> <span class="o">=</span> <span class="n">expr1</span><span class="p">,</span> <span class="n">expr2</span>
</pre></div>
</div>
</div>
<div class="section" id="summary">
<span id="operator-summary"></span><h2>5.14. Summary<a class="headerlink" href="#summary" title="Permalink to this headline">¶</a></h2>
<p id="index-958">The following table summarizes the operator precedences in Python, from lowest
precedence (least binding) to highest precedence (most binding). Operators in
the same box have the same precedence.  Unless the syntax is explicitly given,
operators are binary.  Operators in the same box group left to right (except for
comparisons, including tests, which all have the same precedence and chain from
left to right &#8212; see section <a class="reference internal" href="#comparisons"><em>Comparisons</em></a> &#8212; and exponentiation, which
groups from right to left).</p>
<table border="1" class="docutils">
<colgroup>
<col width="56%" />
<col width="44%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Operator</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><a class="reference internal" href="#lambda"><tt class="xref docutils literal"><span class="pre">lambda</span></tt></a></td>
<td>Lambda expression</td>
</tr>
<tr><td><a class="reference internal" href="#or"><tt class="xref docutils literal"><span class="pre">or</span></tt></a></td>
<td>Boolean OR</td>
</tr>
<tr><td><a class="reference internal" href="#and"><tt class="xref docutils literal"><span class="pre">and</span></tt></a></td>
<td>Boolean AND</td>
</tr>
<tr><td><a class="reference internal" href="#not"><tt class="xref docutils literal"><span class="pre">not</span></tt></a> <em>x</em></td>
<td>Boolean NOT</td>
</tr>
<tr><td><a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a>, <a class="reference internal" href="#not"><tt class="xref docutils literal"><span class="pre">not</span></tt></a> <a class="reference internal" href="#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a>,
<a class="reference internal" href="#is"><tt class="xref docutils literal"><span class="pre">is</span></tt></a>, <a class="reference internal" href="#isnot"><tt class="xref docutils literal"><span class="pre">is</span> <span class="pre">not</span></tt></a>, <tt class="docutils literal"><span class="pre">&lt;</span></tt>,
<tt class="docutils literal"><span class="pre">&lt;=</span></tt>, <tt class="docutils literal"><span class="pre">&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&gt;=</span></tt>, <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt>, <tt class="docutils literal"><span class="pre">!=</span></tt>, <tt class="docutils literal"><span class="pre">==</span></tt></td>
<td>Comparisons, including membership
tests and identity tests,</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">|</span></tt></td>
<td>Bitwise OR</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">^</span></tt></td>
<td>Bitwise XOR</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">&amp;</span></tt></td>
<td>Bitwise AND</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">&lt;&lt;</span></tt>, <tt class="docutils literal"><span class="pre">&gt;&gt;</span></tt></td>
<td>Shifts</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt></td>
<td>Addition and subtraction</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">*</span></tt>, <tt class="docutils literal"><span class="pre">/</span></tt>, <tt class="docutils literal"><span class="pre">//</span></tt>, <tt class="docutils literal"><span class="pre">%</span></tt></td>
<td>Multiplication, division, remainder</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">+x</span></tt>, <tt class="docutils literal"><span class="pre">-x</span></tt>, <tt class="docutils literal"><span class="pre">~x</span></tt></td>
<td>Positive, negative, bitwise NOT</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">**</span></tt></td>
<td>Exponentiation <a class="footnote-reference" href="#id26" id="id18">[8]</a></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">x[index]</span></tt>, <tt class="docutils literal"><span class="pre">x[index:index]</span></tt>,
<tt class="docutils literal"><span class="pre">x(arguments...)</span></tt>, <tt class="docutils literal"><span class="pre">x.attribute</span></tt></td>
<td>Subscription, slicing,
call, attribute reference</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">(expressions...)</span></tt>,
<tt class="docutils literal"><span class="pre">[expressions...]</span></tt>,
<tt class="docutils literal"><span class="pre">{key:datum...}</span></tt>,
<tt class="docutils literal"><span class="pre">`expressions...`</span></tt></td>
<td>Binding or tuple display,
list display,
dictionary display,
string conversion</td>
</tr>
</tbody>
</table>
<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id19" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[1]</a></td><td>In Python 2.3 and later releases, a list comprehension &#8220;leaks&#8221; the control
variables of each <tt class="docutils literal"><span class="pre">for</span></tt> it contains into the containing scope.  However, this
behavior is deprecated, and relying on it will not work in Python 3.0</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id20" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id10">[2]</a></td><td>While <tt class="docutils literal"><span class="pre">abs(x%y)</span> <span class="pre">&lt;</span> <span class="pre">abs(y)</span></tt> is true mathematically, for floats it may not be
true numerically due to roundoff.  For example, and assuming a platform on which
a Python float is an IEEE 754 double-precision number, in order that <tt class="docutils literal"><span class="pre">-1e-100</span> <span class="pre">%</span>
<span class="pre">1e100</span></tt> have the same sign as <tt class="docutils literal"><span class="pre">1e100</span></tt>, the computed result is <tt class="docutils literal"><span class="pre">-1e-100</span> <span class="pre">+</span>
<span class="pre">1e100</span></tt>, which is numerically exactly equal to <tt class="docutils literal"><span class="pre">1e100</span></tt>.  Function <tt class="xref docutils literal"><span class="pre">fmod()</span></tt>
in the <a title="Mathematical functions (sin() etc.)." class="reference external" href="../library/math.html#module-math"><tt class="xref docutils literal"><span class="pre">math</span></tt></a> module returns a result whose sign matches the sign of the
first argument instead, and so returns <tt class="docutils literal"><span class="pre">-1e-100</span></tt> in this case. Which approach
is more appropriate depends on the application.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id21" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id11">[3]</a></td><td>If x is very close to an exact integer multiple of y, it&#8217;s possible for
<tt class="docutils literal"><span class="pre">floor(x/y)</span></tt> to be one larger than <tt class="docutils literal"><span class="pre">(x-x%y)/y</span></tt> due to rounding.  In such
cases, Python returns the latter result, in order to preserve that
<tt class="docutils literal"><span class="pre">divmod(x,y)[0]</span> <span class="pre">*</span> <span class="pre">y</span> <span class="pre">+</span> <span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></tt> be very close to <tt class="docutils literal"><span class="pre">x</span></tt>.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id22" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id13">[4]</a></td><td>While comparisons between unicode strings make sense at the byte
level, they may be counter-intuitive to users. For example, the
strings <tt class="docutils literal"><span class="pre">u&quot;\u00C7&quot;</span></tt> and <tt class="docutils literal"><span class="pre">u&quot;\u0043\u0327&quot;</span></tt> compare differently,
even though they both represent the same unicode character (LATIN
CAPTITAL LETTER C WITH CEDILLA). To compare strings in a human
recognizable way, compare using <a title="unicodedata.normalize" class="reference external" href="../library/unicodedata.html#unicodedata.normalize"><tt class="xref docutils literal"><span class="pre">unicodedata.normalize()</span></tt></a>.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id23" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id14">[5]</a></td><td>The implementation computes this efficiently, without constructing lists or
sorting.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id24" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id15">[6]</a></td><td>Earlier versions of Python used lexicographic comparison of the sorted (key,
value) lists, but this was very expensive for the common case of comparing for
equality.  An even earlier version of Python compared dictionaries by identity
only, but this caused surprises because people expected to be able to test a
dictionary for emptiness by comparing it to <tt class="docutils literal"><span class="pre">{}</span></tt>.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id25" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id16">[7]</a></td><td>Due to automatic garbage-collection, free lists, and the dynamic nature of
descriptors, you may notice seemingly unusual behaviour in certain uses of
the <a class="reference internal" href="#is"><tt class="xref docutils literal"><span class="pre">is</span></tt></a> operator, like those involving comparisons between instance
methods, or constants.  Check their documentation for more info.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id26" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id18">[8]</a></td><td>The power operator <tt class="docutils literal"><span class="pre">**</span></tt> binds less tightly than an arithmetic or
bitwise unary operator on its right, that is, <tt class="docutils literal"><span class="pre">2**-1</span></tt> is <tt class="docutils literal"><span class="pre">0.5</span></tt>.</td></tr>
</tbody>
</table>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">5. Expressions</a><ul>
<li><a class="reference external" href="#arithmetic-conversions">5.1. Arithmetic conversions</a></li>
<li><a class="reference external" href="#atoms">5.2. Atoms</a><ul>
<li><a class="reference external" href="#atom-identifiers">5.2.1. Identifiers (Names)</a></li>
<li><a class="reference external" href="#literals">5.2.2. Literals</a></li>
<li><a class="reference external" href="#parenthesized-forms">5.2.3. Parenthesized forms</a></li>
<li><a class="reference external" href="#list-displays">5.2.4. List displays</a></li>
<li><a class="reference external" href="#generator-expressions">5.2.5. Generator expressions</a></li>
<li><a class="reference external" href="#dictionary-displays">5.2.6. Dictionary displays</a></li>
<li><a class="reference external" href="#string-conversions">5.2.7. String conversions</a></li>
<li><a class="reference external" href="#yield-expressions">5.2.8. Yield expressions</a></li>
</ul>
</li>
<li><a class="reference external" href="#primaries">5.3. Primaries</a><ul>
<li><a class="reference external" href="#attribute-references">5.3.1. Attribute references</a></li>
<li><a class="reference external" href="#subscriptions">5.3.2. Subscriptions</a></li>
<li><a class="reference external" href="#slicings">5.3.3. Slicings</a></li>
<li><a class="reference external" href="#calls">5.3.4. Calls</a></li>
</ul>
</li>
<li><a class="reference external" href="#the-power-operator">5.4. The power operator</a></li>
<li><a class="reference external" href="#unary-arithmetic-and-bitwise-operations">5.5. Unary arithmetic and bitwise operations</a></li>
<li><a class="reference external" href="#binary-arithmetic-operations">5.6. Binary arithmetic operations</a></li>
<li><a class="reference external" href="#shifting-operations">5.7. Shifting operations</a></li>
<li><a class="reference external" href="#binary-bitwise-operations">5.8. Binary bitwise operations</a></li>
<li><a class="reference external" href="#notin">5.9. Comparisons</a></li>
<li><a class="reference external" href="#boolean-operations">5.10. Boolean operations</a></li>
<li><a class="reference external" href="#lambdas">5.11. Lambdas</a></li>
<li><a class="reference external" href="#expression-lists">5.12. Expression lists</a></li>
<li><a class="reference external" href="#evaluation-order">5.13. Evaluation order</a></li>
<li><a class="reference external" href="#summary">5.14. Summary</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="executionmodel.html"
                                  title="previous chapter">4. Execution model</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="simple_stmts.html"
                                  title="next chapter">6. Simple statements</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/reference/expressions.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="simple_stmts.html" title="6. Simple statements"
             >next</a> |</li>
        <li class="right" >
          <a href="executionmodel.html" title="4. Execution model"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Language Reference</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>